package com.SouthernWall_404.AgeSuitcase.Client.Gui.Screens;

import com.SouthernWall_404.AgeSuitcase.Client.Gui.Buttons.TransformableImage;
import com.SouthernWall_404.AgeSuitcase.Common.API.GeneralAPI;
import com.SouthernWall_404.AgeSuitcase.Common.API.GuiAPI.Animation.FadingIn;
import com.SouthernWall_404.AgeSuitcase.Common.API.GuiSoundHelper;
import com.SouthernWall_404.AgeSuitcase.Common.API.Info.LoadingScreenInfo;
import com.SouthernWall_404.AgeSuitcase.Common.init.ModSounds;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.client.sounds.SoundManager;
import net.minecraft.network.chat.Component;

import java.util.Random;

public class LoadingScreen extends Screen {

    public Screen next;
    private static final String GUI= GeneralAPI.GUI+"loadscreen.";


    private SoundInstance soundInstance= SimpleSoundInstance.forUI(ModSounds.GUI_LOADING_TYPING.get(), 1.0F, 1.0F);
    private SoundManager soundManager =Minecraft.getInstance().getSoundManager();


    public int loadingtime;
    private int now=0;
    private int fadingInTime =20;
    private int fadingOutTime =20;
    private FadingIn backGroundFadingIn;
    private boolean isFadingIn=true;
    private boolean isRendering=false;
    private boolean isFadingOut=false;

    private TransformableImage backGround;


    public LoadingScreen(Component p_96550_,Screen screen,int time) {
        super(p_96550_);
        this.next =screen;
        this.loadingtime =time- fadingInTime - fadingOutTime;

//        GuiSoundHelper.playLoadingFinish();
    }

    @Override
    public void tick() {
        super.tick();
        now++;

    }
    public void finish()
    {
        soundManager.stop(soundInstance);
        isFadingOut=false;
        Minecraft.getInstance().setScreen(next);
    }
    public boolean isFinished()
    {
        return now>=loadingtime+fadingInTime+fadingOutTime;
    }
    @Override
    protected void init()
    {
        LoadingScreenInfo loadingScreenInfo=LoadingScreens.screens.get(getRamdom());
        backGround=new TransformableImage(0,0,this.width,this.height,Component.empty(),loadingScreenInfo.path,1920,1057);
//
//        FadingOut backGroundFadingOut=new FadingOut(backGround, fadingOutTime);
//        Wait backGroundWait=new Wait(backGround,loadingtime,backGroundFadingOut);
//        backGroundFadingIn=new FadingIn(backGround,fadingInTime);


    }

    private int fadingInTick=0;
    private int loadingTick=0;
    private int fadingOutTick=0;


    @Override
    public boolean shouldCloseOnEsc() {
        return false;
    }

    @Override
    public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float particularTick) {
        super.render(guiGraphics, mouseX, mouseY, particularTick);
        this.renderBackground(guiGraphics);
//        LoadingScreenInfo loadingScreenInfo=LoadingScreens.screens.get(getRamdom());

        backGround.render(guiGraphics,mouseX,mouseY,particularTick);

        if (isFadingIn)
        {

            fadingInTick++;

            float fadeOutProgress = Math.min(1.0f, (float) fadingInTick /fadingInTime);
            // 绘制半透明黑色覆盖层实现淡出效果
            int alpha = (int) (fadeOutProgress * 200); // 最大透明度为200（约78%）
            guiGraphics.fill(0, 0, this.width, this.height, 1-(alpha << 24));
            if(fadingInTick>=fadingInTime)
            {
                isFadingIn=false;
                isRendering=true;
            }
        }
        else
        {
            if(!soundManager.isActive(soundInstance))
            {
                soundInstance=SimpleSoundInstance.forUI(ModSounds.GUI_LOADING_TYPING.get(), 1.0F, 1.0F);
                soundManager.play(soundInstance);
            }

        }
        if(isRendering)
        {
            loadingTick++;
            if(loadingTick>=loadingtime)
            {
                this.isRendering=false;
                this.isFadingOut=true;
            }
        }
        if (isFadingOut)
        {

            fadingOutTick++;

            float fadeOutProgress = Math.min(1.0f, (float) fadingOutTick /fadingOutTime);
            // 绘制半透明黑色覆盖层实现淡出效果
            int alpha = (int) (fadeOutProgress * 200); // 最大透明度为200（约78%）
            guiGraphics.fill(0, 0, this.width, this.height, (alpha << 24));
            if(fadingOutTick>=fadingOutTime)
            {
                finish();
            }
        }

    }

    @Override
    public void renderBackground(GuiGraphics guiGraphics) {





//        super.renderBackground(p_283688_);
    }
    private int getRamdom()
    {
        Random random=new Random();
        int i=random.nextInt(LoadingScreens.screens.size());
        return i;
    }
}
